\section{Getting Started}

When learning a new programming language, the first program is always to \textit{print} \texttt{hello, world}. In Tactics, we use a subtle different vocabulary: to \textit{write a board game that prints} \texttt{hello, world}. This game consists of only one event, and is very simple:

\begin{verbatim}
enum player { 
        WORLD "The only player -- world"
};

event entry 
{
        output(WORLD, "hello world");
}
\end{verbatim}

Run this program by feeding it to the Tactics interpreter, it will print \texttt{hello, world} to the screen -- actually, the \textit{virtual screen} of the player \texttt{WORLD}.\\

Now, it's time to explain a bit about this very simple program. The most important concept in a Tactics game is \textit{event}. In the simple game above, the block beginning with the keyword \texttt{event}. The statements of an event are enclosed inside a pair of braces.\\

The sample event is named \texttt{entry}. Normally you are at liberty to give events whatever names you like, but \texttt{entry} is special -- your program begins executing at \texttt{entry}. This means that every tactics program must have an \texttt{entry} event.\\

After knowing the event, so what's the magic of this \texttt{enum}?
\begin{verbatim}
enum player { 
        WORLD "The only player -- world"
};
\end{verbatim}
Well, we'll find it out in the next section.\\

Let me show you the road map of our trip first.
\begin{itemize}
\item{Enumeration -- How to represent numbers, suits, and combine them into cards?}
\item{Container -- How to represent the deck and hand cards?}
\end{itemize}

\section{Enumeration}

Basically, enumeration is a data type with its value ranges from a set of tokens. To declare an enumeration:

\begin{verbatim}
enum 'enum_name' {'token', 'token'...};
\end{verbatim}

The declaration of a variable of a certain enumeration type is:

\begin{verbatim}
enum 'enum_name' 'variable_name';
\end{verbatim}

Now let's create an enumeration type. Here the enumeration type \texttt{num} is all the possible number values on a poker card.

\begin{verbatim}
enum num {
        NA = 1 "A",
        N2 "2",
        N3 "3", 
        N4 "4", 
        N5 "5", 
        N6 "6", 
        N7 "7",
        N8 "8",
        N9 "9",
        N10 "10",
        NJ "J", 
        NQ "Q", 
        NK "K"};
\end{verbatim}

The \textit{value} of an enum element is associated with the position it appears in the declaration of the enum type, as well as the integer value after the equal sign. That is, the value of an element in an enum type is larger than the value of an element in the left of it.\\

Then let's create an enumeration type for all the suits.

\begin{verbatim}
enum suit { 	
        SPADES = 1 "Spades", 
        HEARTS "Hearts", 
        DIAMONDS "Diamonds", 
        CLUBS "Clubs"};
\end{verbatim}

Well, actually there is another way to create an enumeration type.

\begin{verbatim}
enum 'enum_name' <'enum_name_1', 'enum_name_2'...>;
\end{verbatim}

Then \texttt{enum\_name} is a \textit{multi-dimensional} enumeration, and the value range is the Cartesian product of all the joining enumerations.\\

So let's create the enumeration for all the cards.
\begin{verbatim}
enum card <num, suit>;
\end{verbatim}

In Tactics, several methods are available on enumerations. For example, we can use the method \texttt{random(enum numbers)} to get a random element from all the possible numbers.\\

Enumeration can also be conveniently used to index players.
\begin{verbatim}
enum player {	
                WANG "Minglei", 
                LU "Chenguang", 
                LIN "Yi", 
                LI "Bo", 
                YANG "Shu"};
\end{verbatim}

Basic enumeration can be incremented or decremented easily. \texttt{player--;} when \texttt{player == WANG} will result in \texttt{player == YANG} and return \texttt{WANG}.\\

What's more powerful is the I/O statements.
\begin{verbatim}
input(WANG, an_int_variable);
output(WANG, "Only Minglei can see this message");
\end{verbatim}
These two statements will have an integer input from player Minglei, and output \texttt{Only Minglei can see this message}, respectively.

\section{Container}

Deck is almost always used in card games -- Let's introduce our powerful contruction tool for it -- container.\\

Container is a constructrion used to store a set of stuff of the same type. We can define a container \texttt{cont\_name} of type \texttt{type} in this way:
\begin{verbatim}
container<'type'> 'cont_name';
\end{verbatim}

So if we want to define a deck as a container of \texttt{card}s, we can write
\begin{verbatim}
container<enum card> deck;
\end{verbatim}

Container is the main data structure in Tactics. It's very generic and can be used for various purposes, and is very convenient since there are some handy methods on it.\\

\texttt{fill(deck)} will fill the container \texttt{deck} with all the possible values of \texttt{enum card}. Notice that this method is only available for containers whose element type is a enumeration.\\

\texttt{shuffle(deck)} is quite straight forward. The shuffle() operation randomly permutes the elements in a container.\\

A card is taken out from a container with remove(). When you pick a card from the deck, it will be removed from the deck, which is exactly what remove(deck, i) does. It deletes the i-1th element of the container and returns it. Try \texttt{temp = remove(deck, top);}.\\

After that we can add element to the container by \texttt{insert(hand[tempplayer], top, temp);}.  The word top is used as a keyword for container which means the first element of the container, similarly the word end means the last element.\\

A particular card can be accessed by calling the method peek. Try \texttt{peek('container\_name', top)} and a card is returned. A random card can be accessed by calling \texttt{peek('container\_name', random)}.


\section{Selection Statement (\texttt{if-else})}

In Tactics, we use an \texttt{if-else} statement as a selection statement.

\begin{verbatim}
if (<condition>) {
        <true_statements>
} else { 
        <true_statements> 
}
\end{verbatim}

If the value of \texttt{condition} is true, \texttt{true\_statement} is executed; otherwise \texttt{false\_statement} is executed. We can also omit the \texttt{else} part, if there is nothing to do when the \texttt{condition} is false.\\

Thus, if we want to compare the value of \texttt{n1} and \texttt{n2} and announce the larger one as the winner, we can use this \texttt{if-else} statement.

\begin{verbatim}
if (n1 > n2) {
        output(WORLD, "n1 wins!");
} else {
        output(WORLD, "n2 wins!");
}
\end{verbatim}

Now we give a complete code of a two-card comparison game, where simply the player with the larger value card wins.

\begin{verbatim}
enum num {      
                NA = 1 "A",		// Define enum for card values
                N2 "2",
                N3 "3",
                N4 "4",
                N5 "5",
                N6 "6",
                N7 "7",
                N8 "8",
                N9 "9",
                N10 "10",
                NJ "J",
                NQ "Q",
                NK "K"};
enum players {	player1 = 1 "player1", 
                player2 "player2"};
enum numbers n1;
enum numbers n2;
event entry { 
                n1=random(enum numbers);
                n2=random(enum numbers);
                if( n1 >= n2){
                        output(player1, "you win");
                } else {
                        output(player2, "you win");		
                }
}
\end{verbatim}

\section{Arrays}

An array is a list of data elements of any type indexed by integers or an enumeration type.
\begin{verbatim}
'type'['index_enum'] 'variable';
\end{verbatim}
or
\begin{verbatim}
'type'['size'] 'variable';
\end{verbatim}

Now, let's see how to make a real array.

\begin{verbatim}
enum players{red, blue}; 
cont<enum card>[enum players] hands;
\end{verbatim}
Remember what cont represent? Yes, that's a container. The \texttt{type}
can be any types in tactics, even a container. What the statement
does is to construct an array of container of Card, and index it by
the enumeration players. Definitely, the array has a size, which is
the size of its indexing enumeration, i.e. 2 in this example. Let's
extend our comparison game with arrays.


\begin{verbatim}
enum num {	
                NA = 1 "A",		// Define enum for card values
                N2 "2",
                N3 "3",
                N4 "4",
                N5 "5",
                N6 "6",
                N7 "7",
                N8 "8",
                N9 "9",
                N10 "10",
                NJ "J",
                NQ "Q",
                NK "K"};
enum players { 	
                player1 = 1 "player1", 
                player2 "player2"};
cont<enum card>[enum players] hand;

event entry { 
                enum numbers n1;
                enum numbers n2;
                fill(hand[player1],enum card);
                fill(hand[player2],enum card);
                shuffle(hand[player1]);
                shuffle(hand[player2]);
		
                do {
                                n1=value(extract(peek(hand[player1])));
                                n2=value(extract(peek(hand[player1])));
                                if( n1 > n2){
                                        output(player1, "you win");
                                } else {
                                        output(player2, "you win");		
                                } 
                        } while (n1==n2);
}
\end{verbatim}
The usage of array is very straightforward: just write


\begin{verbatim}
<array_name>[<index_value>]
\end{verbatim}

\section{Functions}

We now another procedure of Tactics, the function. Though functions
and events look like each other in format, there are significant differences
between them. While events always represent a period or step of a
game, functions are more likely to serve as utilities used by events.
Events can call each other and call events, while functions can only
call otehr functions but not events. Besides, you can never use global
variables in a function. We use call by reference, so it is still convenient to change the global variable in a function.

\begin{verbatim}
int sum(cont<enum card> playerhand) { 
        int sum, i;
        sum = 0;
        for (i = 0; i < size(playerhand); i++) {
        sum = sum + value(extract(peek(playerhand, i), 1)); 
        return sum;
        }
}
\end{verbatim}

The function sum calculates the sum of all the values of a player's
handcards. We can see that a container with element type enum card
is passed as paremeter. The function iterates through the container
and returns the sum. \\

\section{Player Input and Output}

In Tactics, the function input is used for programs to accepts input
from the players, while the function output is used to print information
to the screen of a player.\\

The function input can be used like this:
\begin{verbatim}
input('player_name'), 'destination_variable'>
\end{verbatim}

It stores the input to \texttt{destination\_variable}.

output will print the string literal in the argument to the screen
of a player:
\begin{verbatim}
output(winner, "you win");
\end{verbatim}
In the above statement, it informs the winner that he/she wins.

\section{Putting it all together}

So far, we have introduced all the core components and features of
Tactics. Now let's try to write a simple poker game with Tactics.
The game we use as sample is a variant of the popular casino banking
game BlackJack. The rule is as follows: \\
In our blackjack game, there are 4 players. At the beginning of
the game. Each player is assigned a card from teh deck. For each round,
if one player's hand cards add up to more than 21 points, the game
is over and the players whose handcard exceed 21 lose the game while
the player with the biggest sum of points under or equal to 21 wins.
In each round player can choose to hit or stand. Hit means to ask
for another card while stand means do not want any more cards.\\
The Tactics program below completely described the game.


\begin{verbatim}
enum num {	
                NA = 1 "A",		// Define enum for card values
                N2 "2",
                N3 "3",
                N4 "4",
                N5 "5",
                N6 "6",
                N7 "7",
                N8 "8",
                N9 "9",
                N10 "10",
                NJ "J",
                NQ "Q",
                NK "K"};

enum suit {	
                SPADES = 1 "Spades",	// Define enum for card suits
                HEARTS "Hearts",
                DIAMONDS "Diamonds",
                CLUBS "Clubs"};

enum card <num, suit>;			// Define enum for cards

enum player {	
                WANG "Minglei",		// Define enum for players
                LU "Chenguang",
                LIN "Yi",
                LI "Bo",
                YANG "Shu"};

cont<enum card> deck;			// Card deck
cont<enum card>[enum player] hand;	// Hand cards for each player
enum player current;			// Current player

int sum(cont<enum card> playerhand)
{
        int sum, i;
        sum = 0;

        for (i = 0; i < size(playerhand); i++) {
                sum = sum + value(extract(peek(playerhand, i), 1));
        }
        return sum;
}


event entry {
        call init;
        call newround;
}

event init {
        enum card temp;
        enum player tempplayer;
        fill(deck);
        templayer = WANG;

        if (size(deck) > 0) {
                do {
                        temp = remove(deck, 0);
                        insert(hand[tempplayer], top, temp);
                        templayer++;
                } while (size(deck) > 0 && tempplayer != WANG);
        }
}

event newround {
        bool hit;

        if(sum(hand[current]) > 21) {
                call gameover;
        }

        output(current, "Hit");
        input(current, hit);

        if (hit) {
                call hit;
        } else {
                call stand;	
        }

        call newround;
}

event hit {
        enum card temp;

        if (size(deck) > 0) {
                temp = remove(deck, 0);
                insert(hand[current], top, temp);
        }
}

event stand {
        current++;	
}

event gameover {
        enum player tempplayer;
        enum player winner;
        int min;

        templayer = WANG;
        min = 21;
	
        do {
                if (sum(hand[tempplayer]) > 21) {
                        output(tempplayer, "$tempplayer lose");
                } else {
                        if ((21 - sum(hand[tempplayer])) < min) {
                                min = (21 - sum(hand[tempplayer]));
                                winner = tempplayer;
                        }
                }

                tempplayer++;
        } while (tempplayer != WANG);
	
        output(winner, "you win");
}	
\end{verbatim}
From the sample program one can tell that it is really simple and
fun to map a board game in the real life into Tactics, so why not
start right now to build your own game? Enjoy Tactics , enjoy your
game!\\
